<?php
/**
 * Trieda na routovanie nestandardnych ciest
 * @author Tomas Srna
 * @package bakalarka
 */

class PatternRouter extends Router
{
	protected static $patterns = array();
	
	/**
	 * Routovanie s nahradenim patternov
	 * @param string $request_uri
	 */
	public static function route($request_uri)
	{
		$parts = explode("?", $request_uri, 2);
		
		$befq = $parts[0];
		if(count($parts) > 1)
			$afq = $parts[1];
		else
			$afq = "";
		
		foreach(PatternRouter::$patterns as $pattern => $rewrite)
		{
			if(preg_match($pattern, $befq))
			{
				$befq = preg_replace($pattern, $rewrite, $befq, 1);
				break;
			}
		}
		
		//echo $befq.($afq != ""?'?'.$afq:'');
		
		parent::route($befq.($afq != ""?'?'.$afq:''));
	}
	
	/**
	 * Pridanie rewrite na cestu
	 * @param string Pattern
	 * @param string Rewrite
	 * @return void
	 */
	public static function addPattern($pattern, $rewrite)
	{
		PatternRouter::$patterns[$pattern] = $rewrite;
	}
}
